package javaPersistence;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import beans.ClienteBean;

public class Client {
	
	public boolean insertClient(String code, String nameClient, String addrClient, String phoneClient, String emailClient){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("INSERT INTO CLIENTE VALUES ('");
			query.append(code).append("','").append(nameClient).append("','").append(addrClient).append("','").append(phoneClient).append("','").append(emailClient).append("')");
			System.out.println("query: "+query.toString());
			if(dbConSoft.getDeclaracion().executeUpdate(query.toString()) > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateClient(String code, String nameClient, String addrClient, String phoneClient, String emailClient){
		try {
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder("UPDATE CLIENTE SET nombreCliente = '").append(nameClient).append("', direccionCliente = '").append(addrClient).append("', telefonoCliente = '").append(phoneClient).append("', correoElectronicoCliente = '").append(emailClient).append("' WHERE idCliente = '").append(code).append("'");
			System.out.println("query: "+query.toString());
			if(dbConSoft.getDeclaracion().executeUpdate(query.toString()) > 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public ArrayList<ClienteBean> loadAllClient(){
		ArrayList<ClienteBean> arrClient = new ArrayList<ClienteBean>();
		try {
			ClienteBean clienteB;
			DBConexConSoft dbConSoft = DBConexConSoft.getInstance();
			StringBuilder query = new StringBuilder();
			query.append("SELECT * FROM CLIENTE");
			ResultSet resultSet = dbConSoft.getDeclaracion().executeQuery(query.toString());
			while (resultSet.next()) {
				clienteB = new ClienteBean();
				clienteB.setIdCliente(resultSet.getString("idCliente"));
				clienteB.setNombreCliente(resultSet.getString("nombreCliente"));
				clienteB.setDireccionCliente(resultSet.getString("direccionCliente"));
				clienteB.setTelefonoCliente(resultSet.getString("telefonoCliente"));
				clienteB.setCorreoElectronicoCliente(resultSet.getString("correoElectronicoCliente"));
				arrClient.add(clienteB);
			}
		} catch (SQLException e) {
		}
		return arrClient;
	}
	

}
